﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;

namespace TinyCinema.Data
{
    public class Permisson:Execution
    {
        #region Các chức năng của PM
        bool _Permission;
        bool _Feature;
        bool _Employee;
        bool _Payroll;
        bool _Config;
        bool _CusInfo;
        bool _CusType;
        bool _Movies;
        bool _Schedule;
        bool _SchedulePrice;
        bool _TicketDeal;
        bool _Room;
        bool _Desk;
        bool _MenuInfo;
        bool _MenuDeal;
        bool _Promotion;
        bool _Report;

        public bool IsNotOnlyTicketDealEnabled
        {
            get
            {
                var Temp = TicketDeal && !(_Permission || _Feature || _Employee || _Payroll || _Config || _CusInfo || _CusType || _Movies || _Schedule || _SchedulePrice || _Room || _Desk || _MenuInfo || _MenuDeal || _Promotion || _Report);
                return !Temp;
            }
            set { }
        }
        public bool IsNotOnlyMenuDealEnabled
        {
            get
            {
                var Temp = MenuDeal && !(_TicketDeal || _Permission || _Feature || _Employee || _Payroll || _Config || _CusInfo || _CusType || _Movies || _Schedule || _SchedulePrice || _Room || _Desk || _MenuInfo || _MenuDeal || _Promotion || _Report);
                return !Temp;
            }
            set { }
        }

        public bool AdminPanel
        {
            get { return Permission || Feature; }
            set { }
        }
        public bool HumanResources
        {
            get { return Employee || Payroll || Config; }
            set { }
        }
        public bool Customer
        {
            get { return CusInfo || CusType; }
            set { }
        }
        public bool Schedule
        {
            get { return ScheduleArrangement || SchedulePrice ||TicketDeal; }
            set { }
        }
        public bool Cinema
        {
            get { return Room || Desk; }
            set { }
        }
        public bool Menu
        {
            get { return MenuDeal || MenuInfo; }
            set { }
        }
        public bool Permission
        {
            get { return _Permission; }
            set
            {
                _Permission = value;
                if (!IsIniting) save(1, value);
            }
        }
        public bool Feature
        {
            get { return _Feature; }
            set
            {
                _Feature = value;
                if (!IsIniting) save(2, value);
            }
        }
        public bool Employee
        {
            get { return _Employee; }
            set
            {
                _Employee = value;
                if (!IsIniting) save(3, value);
            }
        }
        public bool Payroll
        {
            get { return _Payroll; }
            set
            {
                _Payroll = value;
                if (!IsIniting) save(4, value);
            }
        }
        public bool Config
        {
            get { return _Config; }
            set
            {
                _Config = value;
                if (!IsIniting) save(5, value);
            }
        }
        public bool CusInfo
        {
            get { return _CusInfo; }
            set
            {
                _CusInfo = value;
                if (!IsIniting) save(6, value);
            }
        }
        public bool CusType
        {
            get { return _CusType; }
            set
            {
                _CusType = value;
                if (!IsIniting) save(7, value);
            }
        }
        public bool Movies
        {
            get { return _Movies; }
            set
            {
                _Movies = value;
                if (!IsIniting) save(8, value);
            }
        }
        public bool ScheduleArrangement
        {
            get { return _Schedule; }
            set
            {
                _Schedule = value;
                if (!IsIniting) save(9, value);
            }
        }
        public bool SchedulePrice
        {
            get { return _SchedulePrice; }
            set
            {
                _SchedulePrice = value;
                if (!IsIniting) save(10, value);
            }
        }
        public bool TicketDeal
        {
            get { return _TicketDeal; }
            set
            {
                _TicketDeal = value;
                if (!IsIniting) save(11, value);
            }
        }
        public bool Room
        {
            get { return _Room; }
            set
            {
                _Room = value;
                if (!IsIniting) save(12, value);
            }
        }
        public bool Desk
        {
            get { return _Desk; }
            set
            {
                _Desk = value;
                if (!IsIniting) save(13, value);
            }
        }
        public bool MenuInfo
        {
            get { return _MenuInfo; }
            set
            {
                _MenuInfo = value;
                if (!IsIniting) save(14, value);
            }
        }
        public bool MenuDeal
        {
            get { return _MenuDeal; }
            set
            {
                _MenuDeal = value;
                if (!IsIniting) save(15, value);
            }
        }
        public bool Promotion
        {
            get { return _Promotion; }
            set
            {
                _Promotion = value;
                if (!IsIniting) save(16, value);
            }
        }
        public bool Report
        {
            get { return _Report; }
            set
            {
                _Report = value;
                if (!IsIniting) save(17, value);
            }
        }

        #endregion
        public string PositionID;
        bool IsIniting = true;
        public Permisson(string _PositionID)
        {
            PositionID = _PositionID;
            PutTableIntoDataSet(Tables.Permission);
            var tbl = ds.Tables[Tables.Permission];
            var Rows = tbl.Select(PermissionTable.PositionID + "='" + PositionID + "'");
            foreach(var Row in Rows)
            {
                int ID = int.Parse((Row[PermissionTable.FeatureID] as string).Substring(2, 6));
                switch(ID)
                {
                    case 1: Permission = true; break;
                    case 2: Feature = true; break;
                    case 3: Employee = true; break;
                    case 4: Payroll = true; break;
                    case 5: Config = true; break;
                    case 6: CusInfo = true; break;
                    case 7: CusType = true; break;
                    case 8: Movies = true; break;
                    case 9: ScheduleArrangement = true; break;
                    case 10: SchedulePrice = true; break;
                    case 11: TicketDeal = true; break;
                    case 12: Room = true; break;
                    case 13: Desk = true; break;
                    case 14: MenuInfo = true; break;
                    case 15: MenuDeal = true; break;
                    case 16: Promotion = true; break;
                    case 17: Report = true; break;
                }
            }
            IsIniting = false;
            
        }
        void save(int ID, bool value)
        {
            string strID = "";
            if (ID < 10) strID = "CN00000" + ID;
            else strID = "CN0000" + ID;
            PutTableIntoDataSet(Tables.Permission);
            var tbl = ds.Tables[Tables.Permission];
            if (value)
            {
                var Row = tbl.NewRow();
                Row[PermissionTable.PositionID] = PositionID;
                Row[PermissionTable.FeatureID] = strID;
                tbl.Rows.Add(Row);
            }
            else
            {
                var Rows = tbl.Select(PermissionTable.PositionID + "='" + PositionID + "' and " + PermissionTable.FeatureID + "='" + strID + "'");
                foreach (var Row in Rows)
                    Row.Delete();
            }
            SaveCommand(Tables.Permission);
        }
    }
}
